<?php

namespace Trawind\Helpers;


use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

class LogHelper
{

    const REDIS_KEY_PREFIX = 'log:operate:';

    public static function record($ids, int $businessTypeId)
    {
        if (!SystemHelper::runningInConsole()) {
            $data = [];
            $ip = request()->getClientIp();
            $app_name = config('app.name');
            $userId = UserHelper::userId(0);
            $ids = is_array($ids) ? $ids : [$ids];
            [$controller, $action] = explode('@', Route::currentRouteAction());
            foreach ($ids as $id) {
                $data[] = [
                    'ip' => $ip,
                    'app_name' => $app_name,
                    'user_id' => $userId,
                    'business_type_id' => $businessTypeId,
                    'business_id' => $id,
                    'controller' => $controller,
                    'action' => $action,
                    'time' => time(),
                ];
            }

            $redis = SystemHelper::redis();
            $redis->set(self::REDIS_KEY_PREFIX.UserHelper::tenantId().':'.Str::uuid(), json_encode($data));


            //            $data = [];
            //            $ip = request()->getClientIp();
            //            $app_name = config('app.name');
            //            $userId = UserHelper::userId(0);
            //            $ids = is_array($ids) ? $ids : [$ids];
            //            $action_id = static::getActionId();
            //
            //            foreach ($ids as $id) {
            //                $data[] = [
            //                    'ip' => $ip,
            //                    'app_name' => $app_name,
            //                    'user_id' => $userId,
            //                    'business_type_id' => $businessTypeId,
            //                    'business_id' => $id,
            //                    'action_id' => $action_id
            //                ];
            //            }
            //
            //            if (class_exists(\TrawindCloud\Repositories\Account\Log\LogOperationRepositoryEloquent::class)) {
            //                return app(\TrawindCloud\Repositories\Account\Log\LogOperationRepositoryEloquent::class)->create($data);
            //            } else {
            //                return app(\Trawind\Remotes\Repositories\Account\Log\LogOperationRepositoryEloquent::class)->create($data);
            //            }
        }
    }

    /**
     * @return int|mixed
     */
    public static function getActionId()
    {
        [$controller, $action] = explode('@', Route::currentRouteAction());

        $resource = app(\Trawind\Remotes\Repositories\User\ResourcesRepositoryEloquent::class)
            ->getListBySearch([
                'search' => "controller_identifier:$controller",
                'searchJoin' => 'and',
                'include' => "resourceOperations:search(action_identifier:$action)"
            ]);

        return $resource['data'][0]['resourceOperations']['data'][0]['id'] ?? 0;
    }


    /**
     * @param $controller
     * @param $actionList
     * @return array
     */
    public static function getActionIdList($controller, $actionList)
    {
        $result = [];
        foreach ($actionList as $action) {
            $resource = app(\Trawind\Remotes\Repositories\User\ResourcesRepositoryEloquent::class)
                ->getListBySearch([
                    'search' => "controller_identifier:$controller",
                    'searchJoin' => 'and',
                    'include' => "resourceOperations:search(action_identifier:$action)"
                ]);
            $result[$action] = $resource['data'][0]['resourceOperations']['data'][0]['id'] ?? 0;
        }
        return $result;
    }

}